//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.editpane;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jfab.core.model.CGMethodDeclaration;

/**
 * Provides an edit pane for a code generator method declaration.
 * 
 * @param <T> Subject type.
 */
public class CGMethodDeclarationEditPane<T extends CGMethodDeclaration> extends
        CGObjectEditPane<T>
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** Return type widget. */
    private JTextField _returnTypeUI;

    /** Throws types widget. */
    private JTextArea _throwsTypesUI;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgMethodDeclaration Code generator method declaration.
     */
    public CGMethodDeclarationEditPane(final T cgMethodDeclaration)
    {
        super(cgMethodDeclaration);
    }

    @Override
    public void applyChanges()
    {
        super.applyChanges();

        final T subject = getSubjectObject();

        subject.setReturnType(getCgTypeAdapter().unmarshal(
                _returnTypeUI.getText()));

        subject.getThrowsTypes().clear();
        subject.getThrowsTypes().addAll(
                getCgTypeListAdapter().unmarshal(_throwsTypesUI.getText()));
    }

    @Override
    protected void createWidgets()
    {
        super.createWidgets();

        _returnTypeUI = createTextField();
        _throwsTypesUI = createTextArea();
    }

    @Override
    protected void fillPanel()
    {
        super.fillPanel();

        bindAndAdd("returnType", _returnTypeUI);
        bindAndAdd("throwsTypes", _throwsTypesUI, getCgTypeAdapter());
    }

    @Override
    protected void fillWidgets()
    {
        super.fillWidgets();

        final T subject = getSubjectObject();

        _returnTypeUI.setText(getCgTypeAdapter().marshal(
                subject.getReturnType()));
        _throwsTypesUI.setText(getCgTypeListAdapter().marshal(
                subject.getThrowsTypes()));
    }
}
